import sys
import os
import random
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        mainLayout = QVBoxLayout() 

        toolBar =QToolBar()
        
        btn = QPushButton('Add Row')
        btn.clicked.connect(self.OnAddRowClicked)
        toolBar.addWidget(btn)

        btn = QPushButton('Add Column')
        btn.clicked.connect(self.OnAddColumnClicked)
        toolBar.addWidget(btn)        

        btn = QPushButton('Delete')
        btn.clicked.connect(self.OnDelClicked)
        toolBar.addWidget(btn)

        toolBar.addWidget(QLabel('Current:'))
        self.currentEdit = QLineEdit()
        toolBar.addWidget(self.currentEdit)
        btn = QPushButton('Update')
        btn.clicked.connect(self.OnUpdateClicked)
        toolBar.addWidget(btn)    

        toolBar.addWidget(QLabel('Keyword:'))
        self.keyEdit = QLineEdit()
        toolBar.addWidget(self.keyEdit)
        btn = QPushButton('Search')
        btn.clicked.connect(self.OnSearchClicked)
        toolBar.addWidget(btn)
        
        self.tableWidget = QTableWidget(8,3)
        self.tableWidget.setHorizontalHeaderLabels(['C1','C2','C3'])     
        self.tableWidget.currentItemChanged.connect(self.OnCurrentItemChanged)
        mainLayout.addWidget(toolBar)
        mainLayout.addWidget(self.tableWidget)
        self.setLayout(mainLayout) 
    def OnCurrentItemChanged(self,current,pre):
        if current is None:
            self.currentEdit.setText(None)
        else:
            self.currentEdit.setText(current.text())
            self.parent.OnShowMessage(f"Current Item '{current.text()}'")
    def OnAddRowClicked(self):        
        self.tableWidget.setRowCount(self.tableWidget.rowCount()+1)
        self.parent.OnShowMessage(f'Add row,count {self.tableWidget.rowCount()}')
    def OnAddColumnClicked(self):        
        columnCount=self.tableWidget.columnCount()
        self.tableWidget.setColumnCount(columnCount+1)
        #self.tableWidget.setHorizontalHeaderItem(columnCount+1,QTableWidgetItem(f'C{columnCount+1}'))#设置无效
        columnName=[]
        for i in range(1,self.tableWidget.columnCount()+1):
            columnName.append(f'C{i}')
        self.tableWidget.setHorizontalHeaderLabels(columnName)
        self.parent.OnShowMessage(f'Add column,count {self.tableWidget.columnCount()}')
    def OnDelClicked(self):
        item = self.tableWidget.currentItem()
        if item is not None:            
            self.parent.OnShowMessage(f"Delete item '{item.text()}'")
            #self.tableWidget.removeCellWidget(item.row(),item.column())
            item.setText(None)
        
    def OnUpdateClicked(self):
        old = self.tableWidget.currentItem().text()
        new = self.currentEdit.text()
        self.tableWidget.currentItem().setText(new)
        self.parent.OnShowMessage(f"Update item from '{old}' to '{new}'")
    def OnSearchClicked(self):
        keyword = self.keyEdit.text()
        msg = f"Searh keyword '{keyword}' result '"
        for item in self.tableWidget.findItems(keyword,Qt.MatchFlag.MatchContains):
            msg += f'{item.text()},'
        msg+="'"
        self.parent.OnShowMessage(msg)
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet